﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;

namespace CommonFunc
{
    public class AutoPage
    {
        
        #region 封装字段
        private int pgIndex;
        private Repeater rPT;
        private DataList dL;
        private DataTable dT;
        private int pageSize;
        private Button btn_FirstPage;
        private Button btn_PrePage;
        private Button btn_NextPage;
        private Button btn_LastPage;
        private LinkButton lBtn_FirstPage;
        private LinkButton lBtn_PrePage;
        private LinkButton lBtn_NextPage;
        private LinkButton lBtn_LastPage;
        private ImageButton iBtn_FirstPage;
        private ImageButton iBtn_PrePage;
        private ImageButton iBtn_NextPage;
        private ImageButton iBtn_LastPage;
        private Label lB_PageIndex;
        private Label lB_PageCount;


        public int PgIndex
        {
            get { return pgIndex; }
            set { pgIndex = value; }
        }

        public Repeater RPT
        {
            get { return rPT; }
            set { rPT = value; }
        }

        public DataList DL
        {
            get { return dL; }
            set { dL = value; }
        }

        public DataTable DT
        {
            get { return dT; }
            set { dT = value; }
        }

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        public Button Btn_FirstPage
        {
            get { return btn_FirstPage; }
            set { btn_FirstPage = value; }
        }

        public Button Btn_PrePage
        {
            get { return btn_PrePage; }
            set { btn_PrePage = value; }
        }

        public Button Btn_NextPage
        {
            get { return btn_NextPage; }
            set { btn_NextPage = value; }
        }

        public Button Btn_LastPage
        {
            get { return btn_LastPage; }
            set { btn_LastPage = value; }
        }

        public LinkButton LBtn_FirstPage
        {
            get { return lBtn_FirstPage; }
            set { lBtn_FirstPage = value; }
        }

        public LinkButton LBtn_PrePage
        {
            get { return lBtn_PrePage; }
            set { lBtn_PrePage = value; }
        }

        public LinkButton LBtn_NextPage
        {
            get { return lBtn_NextPage; }
            set { lBtn_NextPage = value; }
        }

        public LinkButton LBtn_LastPage
        {
            get { return lBtn_LastPage; }
            set { lBtn_LastPage = value; }
        }

        public ImageButton IBtn_FirstPage
        {
            get { return iBtn_FirstPage; }
            set { iBtn_FirstPage = value; }
        }

        public ImageButton IBtn_PrePage
        {
            get { return iBtn_PrePage; }
            set { iBtn_PrePage = value; }
        }

        public ImageButton IBtn_NextPage
        {
            get { return iBtn_NextPage; }
            set { iBtn_NextPage = value; }
        }

        public ImageButton IBtn_LastPage
        {
            get { return iBtn_LastPage; }
            set { iBtn_LastPage = value; }
        }

        public Label LB_PageIndex
        {
            get { return lB_PageIndex; }
            set { lB_PageIndex = value; }
        }

        public Label LB_PageCount
        {
            get { return lB_PageCount; }
            set { lB_PageCount = value; }
        }
        #endregion


        #region Repeater自动分页初始化函数
        /// <summary>
        /// Repeater自动分页ForButton
        /// </summary>
        /// <param name="_RPT"></param>
        /// <param name="_DT"></param>
        /// <param name="_PageSize"></param>
        /// <param name="_Btn_FirstPage"></param>
        /// <param name="_Btn_PrePage"></param>
        /// <param name="_Btn_NextPage"></param>
        /// <param name="_Btn_LastPage"></param>
        /// <param name="_LB_PageIndex"></param>
        /// <param name="_LB_PageCount"></param>
        public void AutoPageInit(Repeater _RPT, 
            DataTable _DT, int _PageSize, 
            Button _Btn_FirstPage, Button _Btn_PrePage, 
            Button _Btn_NextPage, Button _Btn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.RPT = _RPT;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.Btn_FirstPage = _Btn_FirstPage;
            this.Btn_PrePage = _Btn_PrePage;
            this.Btn_NextPage = _Btn_NextPage;
            this.Btn_LastPage = _Btn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.Btn_FirstPage.Click += new EventHandler(FirstPage_Click);
            this.Btn_PrePage.Click += new EventHandler(PrePage_Click);
            this.Btn_NextPage.Click += new EventHandler(NextPage_Click);
            this.Btn_LastPage.Click += new EventHandler(LastPage_Click);
        }
        /// <summary>
        /// Repeater自动分页ForLinkButton
        /// </summary>
        /// <param name="_RPT"></param>
        /// <param name="_DT"></param>
        /// <param name="_PageSize"></param>
        /// <param name="_LBtn_FirstPage"></param>
        /// <param name="_LBtn_PrePage"></param>
        /// <param name="_LBtn_NextPage"></param>
        /// <param name="_LBtn_LastPage"></param>
        /// <param name="_LB_PageIndex"></param>
        /// <param name="_LB_PageCount"></param>
        public void AutoPageInit(Repeater _RPT,
            DataTable _DT, int _PageSize,
            LinkButton _LBtn_FirstPage, LinkButton _LBtn_PrePage,
            LinkButton _LBtn_NextPage, LinkButton _LBtn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.RPT = _RPT;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.LBtn_FirstPage = _LBtn_FirstPage;
            this.LBtn_PrePage = _LBtn_PrePage;
            this.LBtn_NextPage = _LBtn_NextPage;
            this.LBtn_LastPage = _LBtn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.LBtn_FirstPage.Click += new EventHandler(FirstPage_Click);
            this.LBtn_PrePage.Click += new EventHandler(PrePage_Click);
            this.LBtn_NextPage.Click += new EventHandler(NextPage_Click);
            this.LBtn_LastPage.Click += new EventHandler(LastPage_Click);
        }
        /// <summary>
        /// Repeater自动分页ForImageButton
        /// </summary>
        /// <param name="_RPT"></param>
        /// <param name="_DT"></param>
        /// <param name="_PageSize"></param>
        /// <param name="_IBtn_FirstPage"></param>
        /// <param name="_IBtn_PrePage"></param>
        /// <param name="_IBtn_NextPage"></param>
        /// <param name="_IBtn_LastPage"></param>
        /// <param name="_LB_PageIndex"></param>
        /// <param name="_LB_PageCount"></param>
        public void AutoPageInit(Repeater _RPT,
            DataTable _DT, int _PageSize,
            ImageButton _IBtn_FirstPage, ImageButton _IBtn_PrePage,
            ImageButton _IBtn_NextPage, ImageButton _IBtn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.RPT = _RPT;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.IBtn_FirstPage = _IBtn_FirstPage;
            this.IBtn_PrePage = _IBtn_PrePage;
            this.IBtn_NextPage = _IBtn_NextPage;
            this.IBtn_LastPage = _IBtn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.IBtn_FirstPage.Click += new ImageClickEventHandler(FirstPage_Click);
            this.IBtn_PrePage.Click += new ImageClickEventHandler(PrePage_Click);
            this.IBtn_NextPage.Click += new ImageClickEventHandler(NextPage_Click);
            this.IBtn_LastPage.Click += new ImageClickEventHandler(LastPage_Click);
        }
        #endregion


        #region DataList自动分页初始化函数
        /// <summary>
        /// DataList自动分页ForButton
        /// </summary>
        /// <param name="_DL"></param>
        /// <param name="_DT"></param>
        /// <param name="_PageSize"></param>
        /// <param name="_Btn_FirstPage"></param>
        /// <param name="_Btn_PrePage"></param>
        /// <param name="_Btn_NextPage"></param>
        /// <param name="_Btn_LastPage"></param>
        /// <param name="_LB_PageIndex"></param>
        /// <param name="_LB_PageCount"></param>
        public void AutoPageInit(DataList _DL, 
            DataTable _DT, int _PageSize, 
            Button _Btn_FirstPage, Button _Btn_PrePage, 
            Button _Btn_NextPage, Button _Btn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.DL = _DL;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.Btn_FirstPage = _Btn_FirstPage;
            this.Btn_PrePage = _Btn_PrePage;
            this.Btn_NextPage = _Btn_NextPage;
            this.Btn_LastPage = _Btn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.Btn_FirstPage.Click += new EventHandler(FirstPage_Click);
            this.Btn_PrePage.Click += new EventHandler(PrePage_Click);
            this.Btn_NextPage.Click += new EventHandler(NextPage_Click);
            this.Btn_LastPage.Click += new EventHandler(LastPage_Click);
        }
        /// <summary>
        /// DataList自动分页ForLinkButton
        /// </summary>
        /// <param name="_DL"></param>
        /// <param name="_DT"></param>
        /// <param name="_PageSize"></param>
        /// <param name="_LBtn_FirstPage"></param>
        /// <param name="_LBtn_PrePage"></param>
        /// <param name="_LBtn_NextPage"></param>
        /// <param name="_LBtn_LastPage"></param>
        /// <param name="_LB_PageIndex"></param>
        /// <param name="_LB_PageCount"></param>
        public void AutoPageInit(DataList _DL,
            DataTable _DT, int _PageSize,
            LinkButton _LBtn_FirstPage, LinkButton _LBtn_PrePage,
            LinkButton _LBtn_NextPage, LinkButton _LBtn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.DL = _DL;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.LBtn_FirstPage = _LBtn_FirstPage;
            this.LBtn_PrePage = _LBtn_PrePage;
            this.LBtn_NextPage = _LBtn_NextPage;
            this.LBtn_LastPage = _LBtn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.LBtn_FirstPage.Click += new EventHandler(FirstPage_Click);
            this.LBtn_PrePage.Click += new EventHandler(PrePage_Click);
            this.LBtn_NextPage.Click += new EventHandler(NextPage_Click);
            this.LBtn_LastPage.Click += new EventHandler(LastPage_Click);


        }

        public void AutoPageInit(DataList _DL,
            DataTable _DT, int _PageSize,
            ImageButton _IBtn_FirstPage, ImageButton _IBtn_PrePage,
            ImageButton _IBtn_NextPage, ImageButton _IBtn_LastPage,
            Label _LB_PageIndex, Label _LB_PageCount)
        {
            this.DL = _DL;
            this.DT = _DT;
            this.PageSize = _PageSize;
            this.IBtn_FirstPage = _IBtn_FirstPage;
            this.IBtn_PrePage = _IBtn_PrePage;
            this.IBtn_NextPage = _IBtn_NextPage;
            this.IBtn_LastPage = _IBtn_LastPage;
            this.LB_PageIndex = _LB_PageIndex;
            this.LB_PageCount = _LB_PageCount;

            this.IBtn_FirstPage.Click += new ImageClickEventHandler(FirstPage_Click);
            this.IBtn_PrePage.Click += new ImageClickEventHandler(PrePage_Click);
            this.IBtn_NextPage.Click += new ImageClickEventHandler(NextPage_Click);
            this.IBtn_LastPage.Click += new ImageClickEventHandler(LastPage_Click);
        }
        #endregion


        #region [首页] [上一页] [下一页] [末页]  四个按钮的事件
        protected void FirstPage_Click(object sender, EventArgs e)
        {
            if (this.RPT != null)
            {
                RPTAutoPage(0);
            }
            else
            {
                DLAutoPage(0);
            }
        }
        protected void PrePage_Click(object sender, EventArgs e)
        {
            if (this.RPT != null)
            {
                RPTAutoPage(Convert.ToInt32(this.LB_PageIndex.Text) - 2);
            }
            else
            {
                DLAutoPage(Convert.ToInt32(this.LB_PageIndex.Text) - 2);
            }
        }
        protected void NextPage_Click(object sender, EventArgs e)
        {
            if (this.RPT != null)
            {
                RPTAutoPage(Convert.ToInt32(this.LB_PageIndex.Text));
            }
            else
            {
                DLAutoPage(Convert.ToInt32(this.LB_PageIndex.Text));
            }
        }
        protected void LastPage_Click(object sender, EventArgs e)
        {
            if (this.RPT != null)
            {
                RPTAutoPage(Convert.ToInt32(this.lB_PageCount.Text) - 1);
            }
            else
            {
                DLAutoPage(Convert.ToInt32(this.lB_PageCount.Text) - 1);
            }
        }
        #endregion


        #region Repeater自动分页数据绑定
        /// <summary>
        /// Repeater自动分页数据绑定
        /// </summary>
        /// <param name="_PageIndex">要显示页面的索引</param>
        public void RPTAutoPage(int _PageIndex)
        {
            PagedDataSource PDS = new PagedDataSource();
            PDS.DataSource = this.DT.DefaultView;

            PDS.AllowPaging = true;
            PDS.PageSize = this.PageSize;
            PDS.CurrentPageIndex = _PageIndex;

            this.RPT.DataSource = PDS;

            this.LB_PageIndex.Text = Convert.ToString(_PageIndex + 1);
            this.LB_PageCount.Text = PDS.PageCount.ToString();

            if (this.Btn_FirstPage != null)
            {
                this.Btn_FirstPage.Enabled = true;
                this.Btn_PrePage.Enabled = true;
                this.Btn_NextPage.Enabled = true;
                this.Btn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.Btn_FirstPage.Enabled = false;
                    this.Btn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.Btn_NextPage.Enabled = false;
                    this.Btn_LastPage.Enabled = false;
                }
            }
            else if (this.LBtn_FirstPage != null)
            {
                this.LBtn_FirstPage.Enabled = true;
                this.LBtn_PrePage.Enabled = true;
                this.LBtn_NextPage.Enabled = true;
                this.LBtn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.LBtn_FirstPage.Enabled = false;
                    this.LBtn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.LBtn_NextPage.Enabled = false;
                    this.LBtn_LastPage.Enabled = false;
                }
            }
            else
            {
                this.IBtn_FirstPage.Enabled = true;
                this.IBtn_PrePage.Enabled = true;
                this.IBtn_NextPage.Enabled = true;
                this.IBtn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.IBtn_FirstPage.Enabled = false;
                    this.IBtn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.IBtn_NextPage.Enabled = false;
                    this.IBtn_LastPage.Enabled = false;
                }
            }

            this.RPT.DataBind();
        }
        #endregion


        #region DataList自动分页数据绑定函数
        /// <summary>
        /// DataList自动分页数据绑定函数
        /// </summary>
        /// <param name="_PageIndex">要显示页面的索引</param>
        public void DLAutoPage(int _PageIndex)
        {
            PagedDataSource PDS = new PagedDataSource();
            PDS.DataSource = this.DT.DefaultView;

            PDS.AllowPaging = true;
            PDS.PageSize = this.PageSize;
            PDS.CurrentPageIndex = _PageIndex;

            this.DL.DataSource = PDS;

            this.LB_PageIndex.Text = Convert.ToString(_PageIndex + 1);
            this.LB_PageCount.Text = PDS.PageCount.ToString();

            if (this.Btn_FirstPage != null)
            {
                this.Btn_FirstPage.Enabled = true;
                this.Btn_PrePage.Enabled = true;
                this.Btn_NextPage.Enabled = true;
                this.Btn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.Btn_FirstPage.Enabled = false;
                    this.Btn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.Btn_NextPage.Enabled = false;
                    this.Btn_LastPage.Enabled = false;
                }
            }
            else if (this.LBtn_FirstPage != null)
            {
                this.LBtn_FirstPage.Enabled = true;
                this.LBtn_PrePage.Enabled = true;
                this.LBtn_NextPage.Enabled = true;
                this.LBtn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.LBtn_FirstPage.Enabled = false;
                    this.LBtn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.LBtn_NextPage.Enabled = false;
                    this.LBtn_LastPage.Enabled = false;
                }
            }
            else
            {
                this.IBtn_FirstPage.Enabled = true;
                this.IBtn_PrePage.Enabled = true;
                this.IBtn_NextPage.Enabled = true;
                this.IBtn_LastPage.Enabled = true;

                if (PDS.CurrentPageIndex == 0)
                {
                    this.IBtn_FirstPage.Enabled = false;
                    this.IBtn_PrePage.Enabled = false;
                }
                if (PDS.CurrentPageIndex == (PDS.PageCount - 1))
                {
                    this.IBtn_NextPage.Enabled = false;
                    this.IBtn_LastPage.Enabled = false;
                }
            }

            this.DL.DataBind();
        }
        #endregion
    }
}
